package com.example.memohehang.service.Impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.memohehang.common.R;
import com.example.memohehang.domain.Notes;
import com.example.memohehang.mapper.NotesMapper;
import com.example.memohehang.service.NotesService;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NotesServiceImpl extends ServiceImpl<NotesMapper, Notes> implements NotesService
{
    private NotesMapper notesMapper;
    /**
     * 添加备忘录
     * @param notes
     * @return
     */
    @Override
    public R addNote(Notes notes)
    {
        boolean b = save(notes);//调用mybatis-plus
        if (b)
        {
            return R.success(200, "添加成功", null);
        }
        else
        {
            return R.error(405, "添加失败");
        }

    }

    /**
     * 根据noteid查询note
     * @param noteid
     * @return
     */
    @Override
    public R selectByNote(Integer noteid)
    {
        QueryWrapper<Notes> wrapper = new QueryWrapper<>();

        wrapper.eq("id", noteid);
        List<Notes> note =  list(wrapper);

        if (note == null)
        {
            return R.error(400,"查询失败");
        }
        else
        {
            return R.success(note.get(0));
        }
    }

    /**
     * 查询 只显示用户自己的
     * @param userid
     * @param currentPage
     * @return
     */
    @Override
    public R selectAllById(Integer userid, Integer currentPage)
    {
        Page page = new Page(currentPage, 20);
        // 查询条件 分页的基础上 再按照创建时间排序
        IPage pageData = page(page, new QueryWrapper<Notes>().orderByAsc("finish","time").eq("rid", userid));

        return R.success(pageData);
    }

    /**
     * 查询最近即将截止的未完成的备忘录的时间差
     * @param userid
     * @return
     */
    @Override
    public R selectcutDownTime(Integer userid)
    {
        QueryWrapper<Notes> wrapper = new QueryWrapper<>();
        wrapper.eq("rid", userid).orderByAsc("finish", "time");
        List<Notes> note = list(wrapper);

        // 处理查询结果
        if (note != null && !note.isEmpty())
        {
            Notes firstUnfinishedNote = null;
            for (Notes n : note)
            {
                if (n.getFinish() == 0)
                {
                    firstUnfinishedNote = n;
                    break;
                }
            }

            if (firstUnfinishedNote != null)
            {
                LocalDateTime currentTime = LocalDateTime.now();
                Date noteTime = firstUnfinishedNote.getTime();

                Instant instant = noteTime.toInstant();
                LocalDateTime noteLocalDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();

                Duration duration = Duration.between(currentTime, noteLocalDateTime);
                long days = duration.toDays();
                long hours = duration.toHoursPart();
                long minutes = duration.toMinutesPart();
                long seconds = duration.toSecondsPart();

                // 构造返回结果
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("days", days);
                resultMap.put("hours", hours);
                resultMap.put("minutes", minutes);
                resultMap.put("seconds", seconds);

                System.out.println(resultMap);
                // 返回结果
                return R.success(200, "duration", resultMap);
            }
            else
            {
                return R.error(405, "没有找到未完成的备忘录");
            }
        }
        else
        {
            return R.error(405, "没有找到备忘录");
        }
    }

    /**
     * 分类查询
     * @param userid
     * @param type
     * @param isFinish
     * @param currentPage
     * @return
     */
    @Override
    public R selectByType(Integer userid, Integer type, Integer isFinish, Integer currentPage)
    {
        Page page = new Page(currentPage, 20);

        IPage pageData = page(page, new QueryWrapper<Notes>().orderByAsc("time").eq("rid", userid).eq("finish", isFinish).eq("type", type));

        return R.success(pageData);
    }

    /**
     * 修改
     * @param notes
     * @return
     */
    @Override
    public R update(Notes notes)
    {
        System.out.println("开始更新");
        System.out.println(notes);
        boolean b = update(notes, new QueryWrapper<Notes>().eq("id", notes.getId()));
        if (b)
        {
            System.out.println("更新成功");
            return R.success(200, "更新成功", null);
        }
        System.out.println("更新失败");
        return R.error(405, "更新失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public R delete(Integer id)
    {
        System.out.println("user delete..." + id);
        boolean b = removeById(id);
        if (b)
        {
            return R.success(200, "删除成功", null);
        }
        return R.error(405, "删除失败");
    }
}

